/**
 * location 对象扩展
 * @param {*} location
 */
var locationExtender = function (location) {
  if (location === null || location === undefined) {
    location = window.location;
  }

  //扩展 origin 对象
  if (location.origin === null || location.origin === undefined) {
    var origin = location.protocol + "//" + location.host;
    location.origin = origin;
  }
  //扩展路径名数组对象
  var pathnameArray = location.pathname.split("/");
  var pathnameArray2 = [];
  for (var i = 0; i < pathnameArray.length; i++) {
    var pathItem = pathnameArray[i];
    if (pathItem === "") {
      continue;
    }
    pathnameArray2.push(pathItem);
  }
  pathnameArray = pathnameArray2;
  location.pathnameArray = pathnameArray;

  //扩展 search 的数据读取
  var searchText = location.search;
  searchText = searchText.substring(1);
  var searchTextArray =[];
  if(searchText!==""){
      searchTextArray = searchText.split("&");
  }
  var searchObj = {};
  for (var i = 0; i < searchTextArray.length; i++) {
    var kv = searchTextArray[i].split("=");
    var k = kv[0];
    var v = kv[1];
    searchObj[k] = v;
  }
  location.searchObj = searchObj;
  /**
   * 根据查询key获取key对应的值
   * @param {string} key 查询key（不区分大小写）
   */
  location.getSearchValue = function (key) {
    var object = location.searchObj;
    var val = "";
    for (var k in object) {
      if (
        object.hasOwnProperty(k) &&
        k.toLocaleLowerCase() === key.toLocaleLowerCase()
      ) {
        val = object[k];
      }
    }
    return val;
  };
};

locationExtender();
